package com.example.phoneshop.controller.admin;

import com.example.phoneshop.config.Config;
import com.example.phoneshop.controller.OrderController;
import com.example.phoneshop.entity.Order;
import com.example.phoneshop.entity.OrderGoods;
import com.example.phoneshop.entity.OrderPage;
import com.example.phoneshop.service.OrderService;
import com.example.phoneshop.tool.Check;
import com.example.phoneshop.tool.ResultJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


/**
 * @author nozomi
 * @date 2018/7/10 19:00
 * @description
 */
@Controller
@RequestMapping("/admin")
public class AdminOrderController {
    private OrderService orderService;
    @Autowired
    public AdminOrderController(OrderService orderService){
        this.orderService=orderService;
    }

    /**
     * 获取订单列表
     * @param currentPage
     * @param request
     * @return
     */
    @RequestMapping(value = "/orderList",method = RequestMethod.GET)
    public ModelAndView getOrderList(@RequestParam(value = "currentPage",defaultValue = "1",required = false)int currentPage,
                                     HttpServletRequest request, HttpSession session, HttpServletResponse response){
        if (isAdminLogin(session, response)) {
            return null;
        }
        String[] menu;
        ModelAndView model = new ModelAndView("orders/order_info");
        // 获取导航栏的active数组
        menu = Config.getMenu();
        model.addObject("user",session.getAttribute("user"));
        model.addObject("title", "订单信息");
        model.addObject("classurl",  request.getContextPath()+ "/admin/index");
        model.addObject("classname", "订单管理");
        menu[8] = "open active";
        menu[9] = "active";
        model.addObject("menu", menu);
        //将时间戳转化为格式化的String显示
        OrderPage<Order> pageMsg=orderService.findOrderByPage(currentPage);
        List<Order> list=pageMsg.getLists();
        OrderController.getFormatTime(list);
        model.addObject("pageMsg",pageMsg);
        model.addObject("currentPage",currentPage);
        model.addObject("totalPage",pageMsg.getTotalPage());
        return model;
    }

    /**
     * 更改订单状态
     * @param id
     * @return
     */
    @RequestMapping(value = "/deliver",method = RequestMethod.POST)
    public ModelAndView deliverGoods(int id){
        ResultJson resultJson=new ResultJson();
        Order order=orderService.selectById(id);
        String state="已发货";
        if(state.equals(order.getOrderState())){
           resultJson.setText("该订单已经发货");
        }
        else {
            order.setOrderState("已发货");
            orderService.updateState(order);
            resultJson.setText("ok");
        }
        return new ModelAndView("redirect:/admin/orderList");
    }


    /**
     * 判断是否登录
     * @param session
     * @param response
     * @return
     */
    public static boolean isAdminLogin(HttpSession session, HttpServletResponse response) {
        if (!Check.checkAdminLogin(session)) {
            try {
                response.sendRedirect("/admin/login");
                return true;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return false;
    }
}
